#include "client_func.h"

int tcp_client_init()
{
    strcpy(tcp_ip, "192.168.149.128");
    strcpy(tcp_port, "2000");

    // 1. 生成套接字描述符
    sfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(sfd, -1, "socket");

    struct sockaddr_in ser_addr; // 服务器信息结构体
    bzero(&ser_addr, sizeof(ser_addr));
    ser_addr.sin_family = AF_INET; // 使用IPv4
    ser_addr.sin_port = htons(atoi(tcp_port)); // 端口号
    ser_addr.sin_addr.s_addr = inet_addr(tcp_ip); // IP地址

    // 2. connect
    ret = connect(sfd, (struct sockaddr *)&ser_addr, sizeof(ser_addr));
    ERROR_CHECK(ret, -1, "connect");

    is_running = 1;
}

int start()
{
    while(is_running)
    {
        memset(&t_s_data, 0, sizeof(t_s_data));
        setbuf(stdin, NULL);
        fgets(t_s_data.buf, 128, stdin);
        t_s_data.buf[strlen(t_s_data.buf) - 1] = '\0';
        printf("you input command: %s\n", t_s_data.buf);

        if (!strncmp(t_s_data.buf, "register", 8))
        {
            do_register();
        }
        else if (!strncmp(t_s_data.buf, "login", 5))
        {
            do_login();
        }
        else if (!strncmp(t_s_data.buf, "cd", 2))
        {
            do_cd();
        }
        else if(!strncmp(t_s_data.buf, "ls", 2))
        {
            do_ls();
        }
        else if (!strncmp(t_s_data.buf, "puts", 4))
        {
            do_puts();
        }
        else if (!strncmp(t_s_data.buf, "gets", 4))
        {
            do_gets();
        }
        else if(!strncmp(t_s_data.buf, "remove", 6))
        {
            do_remove();
        }
        else if(!strcmp(t_s_data.buf, "pwd"))
        {
            do_pwd();
        }
        else
        {
            do_default();
        }
    }
}

int do_register()
{
    char username[100] = {0};
    char raw_password[100] = {0};
    printf("username: ");
    scanf("%s", username);
    printf("password: ");
    scanf("%s", raw_password);
    
    memset(&t_s_data, 0, sizeof(t_s_data));
    sprintf(t_s_data.buf, "%s %s", username, raw_password);
    printf("t_s_data.buf = %s\n", t_s_data.buf);
    t_s_data.state = REGISTER;
    send(sfd, &t_s_data, sizeof(t_s_data), 0);

    memset(&r_data, 0, sizeof(r_data));
    recv(sfd, &r_data, sizeof(r_data), 0);
    printf("%s\n", r_data.buf);

    return 0;
}

int do_login()
{
    char username[100] = {0};
    char raw_password[100] = {0};
    printf("username: ");
    scanf("%s", username);
    printf("password: ");
    scanf("%s", raw_password);

    memset(&t_s_data, 0, sizeof(t_s_data));
    sprintf(t_s_data.buf, "%s %s", username, raw_password);
    printf("t_s_data.buf = %s\n", t_s_data.buf);
    t_s_data.state = LOGIN;
    send(sfd, &t_s_data, sizeof(t_s_data), 0);

    memset(&r_data, 0, sizeof(r_data));
    recv(sfd, &r_data, sizeof(r_data), 0);
    printf("%s\n", r_data.buf);

    return 0;
}

int do_cd()
{
    t_s_data.state = CD;
    send(sfd, &t_s_data, sizeof(t_s_data), 0);


    memset(&r_data, 0, sizeof(r_data));
    recv(sfd, &r_data, sizeof(r_data), 0);

    printf("%s\n", r_data.buf);
    if (r_data.data_len == 0)
    {
        
    }
    

    return 0;
}

int do_ls()
{
    t_s_data.state = LS;
    send_n(sfd, &t_s_data, sizeof(t_s_data));

    memset(&r_data, 0, sizeof(r_data));
    recv(sfd, &r_data, sizeof(r_data), 0);
    system("clear");
    printf("%s\n", r_data.buf);
    return 0;
}

int do_puts()
{
    struct stat buf;
    ret = stat(t_s_data.buf + 5, &buf);

    if (S_ISDIR(buf.st_mode) || ret == -1)
    {
        printf("file dont existence or file is a folder\n");
        return -1;
    }
    else
    {
        t_s_data.state = PUTS;
        send(sfd, &t_s_data, sizeof(t_s_data), 0);


        upload_file(sfd, t_s_data.buf + 5);
        
        memset(&r_data, 0, sizeof(r_data));
        recv(sfd, &r_data, sizeof(r_data), 0);

        printf("%s\n", r_data.buf);

        if (r_data.data_len != -1) {
            return 0;
        }
    }

    
    

    
    
}

int do_gets()
{
    t_s_data.state = GETS;
    send_n(sfd, &t_s_data, sizeof(t_s_data));

    memset(&r_data, 0, sizeof(r_data));
    recv(sfd, &r_data, sizeof(r_data), 0);

    printf("%s\n", r_data.buf);
    if (r_data.data_len != -1)
    {
        int ret = download_file(sfd);
    }

    return 0;
}

int do_remove()
{
    t_s_data.state = REMOVE;
    send_n(sfd, &t_s_data, sizeof(t_s_data));

    memset(&r_data, 0, sizeof(r_data));
    recv(sfd, &r_data, sizeof(r_data), 0);
    printf("%s\n", r_data.buf);
    
    return 0;
}

int do_pwd()
{
    t_s_data.state = PWD;
    send_n(sfd, &t_s_data, sizeof(t_s_data));

    memset(&r_data, 0, sizeof(r_data));
    recv(sfd, &r_data, sizeof(r_data), 0);
    printf("%s\n", r_data.buf);
    
    return 0;
}

int do_default()
{
    printf("wrong command\n");
    return 0;
}

